{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Count Prefixes of a Given String"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: countPrefixes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #统计是给定字符串前缀的字符串数目"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个字符串数组&nbsp;<code>words</code>&nbsp;和一个字符串&nbsp;<code>s</code>&nbsp;，其中&nbsp;<code>words[i]</code> 和&nbsp;<code>s</code>&nbsp;只包含 <strong>小写英文字母</strong>&nbsp;。</p>\n",
    "\n",
    "<p>请你返回 <code>words</code>&nbsp;中是字符串 <code>s</code>&nbsp;<strong>前缀&nbsp;</strong>的 <strong>字符串数目</strong>&nbsp;。</p>\n",
    "\n",
    "<p>一个字符串的 <strong>前缀</strong>&nbsp;是出现在字符串开头的子字符串。<strong>子字符串</strong>&nbsp;是一个字符串中的连续一段字符序列。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>words = [\"a\",\"b\",\"c\",\"ab\",\"bc\",\"abc\"], s = \"abc\"\n",
    "<b>输出：</b>3\n",
    "<strong>解释：</strong>\n",
    "words 中是 s = \"abc\" 前缀的字符串为：\n",
    "\"a\" ，\"ab\" 和 \"abc\" 。\n",
    "所以 words 中是字符串 s 前缀的字符串数目为 3 。</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>words = [\"a\",\"a\"], s = \"aa\"\n",
    "<b>输出：</b>2\n",
    "<strong>解释：\n",
    "</strong>两个字符串都是 s 的前缀。\n",
    "注意，相同的字符串可能在 words 中出现多次，它们应该被计数多次。</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= words.length &lt;= 1000</code></li>\n",
    "\t<li><code>1 &lt;= words[i].length, s.length &lt;= 10</code></li>\n",
    "\t<li><code>words[i]</code> 和&nbsp;<code>s</code>&nbsp;<strong>只</strong>&nbsp;包含小写英文字母。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [count-prefixes-of-a-given-string](https://leetcode.cn/problems/count-prefixes-of-a-given-string/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [count-prefixes-of-a-given-string](https://leetcode.cn/problems/count-prefixes-of-a-given-string/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[\"a\",\"b\",\"c\",\"ab\",\"bc\",\"abc\"]\\n\"abc\"', '[\"a\",\"a\"]\\n\"aa\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        ans = 0 \n",
    "        for word in words:\n",
    "            if s[:len(word)] == word:\n",
    "                ans += 1\n",
    "        \n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        n = len(s)\n",
    "        result = 0\n",
    "        for i in range(1, n+1):\n",
    "            result = result + words.count(s[0:i])\n",
    "        return result \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        n = 0\n",
    "        for w in words:\n",
    "            if s.startswith(w):\n",
    "                n += 1\n",
    "        return n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        res = 0\n",
    "        for i in words :\n",
    "            if len(i) >len(s):\n",
    "                continue\n",
    "            else:\n",
    "                for j in range(len(i)):\n",
    "                    if i[j] == s[j]:\n",
    "                        continue\n",
    "                    else:\n",
    "                        res -=1\n",
    "                        break\n",
    "                res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self,words: List[str], s: str) -> int:\n",
    "        count=0\n",
    "        for a in words:\n",
    "            if len(a)>len(s):\n",
    "                continue\n",
    "            elif a==s[:len(a)]:\n",
    "                count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        res = 0   # 符合要求字符串个数\n",
    "        for word in words:\n",
    "            if s.startswith(word):\n",
    "                res += 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        data_list = []\n",
    "        for i in range(0,len(s)):\n",
    "            data_list.append(s[:i+1])\n",
    "        \n",
    "        i = 0\n",
    "        for data in words:\n",
    "            if data in data_list:\n",
    "                i +=1\n",
    "        \n",
    "        return i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        res = 0\n",
    "        for i in range(len(words)):\n",
    "            if len(words[i]) > len(s):\n",
    "                continue\n",
    "            flag = True\n",
    "            for j in range(len(words[i])):\n",
    "                if words[i][j] != s[j]:\n",
    "                    flag = False\n",
    "                    break\n",
    "            \n",
    "            if flag:\n",
    "                res += 1\n",
    "        \n",
    "        return res\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self,words: List[str], s: str) -> int:\n",
    "        count=0\n",
    "        for a in words:\n",
    "            check=True\n",
    "            if len(a)<=len(s):\n",
    "                for b in range(len(a)):\n",
    "                    if a[b]!=s[b]:\n",
    "                        check=False\n",
    "                        break\n",
    "            else:\n",
    "                check=False\n",
    "                continue\n",
    "            if check==True:\n",
    "                count=count+1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        return sum([words.count(s[:i + 1]) for i in range(len(s))])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        count=0\n",
    "        for i in words:\n",
    "            if s.startswith(i):\n",
    "                count+=1\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        count = 0\n",
    "        for i in words:\n",
    "            if s.startswith(i):\n",
    "                count += 1\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        return sum(1 for w in words if s.startswith(w))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        result=0\n",
    "        for i in range(len(words)):\n",
    "            if s[:len(words[i])]==words[i]:\n",
    "                result+=1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        res = 0\n",
    "        for i in words:\n",
    "            if s.startswith(i):\n",
    "                res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        p=1\n",
    "        o=0\n",
    "        for i in range(len(words)):\n",
    "            if words[i]==s[0:len(words[i])]:\n",
    "                o+=1\n",
    "        return o\n",
    "\n",
    "\n",
    "            \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        a=[s[:i] for i in range(1,len(s)+1)]\n",
    "        c=0\n",
    "        for i in words:\n",
    "            if i in a:\n",
    "                c+=1\n",
    "        return c\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        def judge(s1,s2):\n",
    "            return s1 == s2[:len(s1)]\n",
    "        \n",
    "        return sum(1 for w in words if judge(w,s))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        x=0\n",
    "        for word in words:\n",
    "            if s.startswith(word):\n",
    "                x+=1\n",
    "        return x "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        st = set()\n",
    "        for i in range(len(s)):\n",
    "            st.add(s[:i+1])\n",
    "        return sum(1 for x in words if x in st)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        return sum(s.startswith(w) for w in words)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        def check(word):\n",
    "            if word == s[:len(word)]:\n",
    "                return True\n",
    "            else:\n",
    "                return False\n",
    "        ans = 0\n",
    "        for word in words:\n",
    "            if check(word):\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        #help(str)\n",
    "        count=0\n",
    "        for i in words:\n",
    "            if s.startswith(i):\n",
    "                count+=1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        k = 0\n",
    "        for i in words:\n",
    "            if s.startswith(i):\n",
    "                k+=1\n",
    "        return k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        l=len(s)\n",
    "        sum = 0\n",
    "        for i in words:\n",
    "            l=len(str(i))\n",
    "            if i == s[0:l]:\n",
    "                sum += 1\n",
    "        return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        return sum(s.startswith(i) for i in words)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        res = 0 \n",
    "        isword = True\n",
    "        for word in words:\n",
    "            for s1,s2 in zip(word, s):\n",
    "                if s1 != s2 or len(word) > len(s):\n",
    "                    isword = False\n",
    "                    break\n",
    "            if isword:\n",
    "                res += 1\n",
    "            isword = True\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        return len([1 for x in words if s[:len(x)] == x])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        res = 0\n",
    "        for word in words:\n",
    "            if word == s[: len(word)]:\n",
    "                res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        ans = 0\n",
    "        for w in words:\n",
    "            if s.startswith(w):\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        count = 0\n",
    "        for i in range(1,len(s)+1):\n",
    "            for elem in words:\n",
    "                if elem == s[:i]:\n",
    "                    count += 1\n",
    "\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        dct = dict()\n",
    "        for i in range(len(s)):\n",
    "            dct[s[:i+1]] = 1\n",
    "        cnt = 0\n",
    "        for word in words:\n",
    "            if dct.get(word, 0):\n",
    "                cnt += 1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        num = 0\n",
    "        for w in words:\n",
    "            n = len(w)\n",
    "            if s[:n]==w:\n",
    "                num+=1\n",
    "        return (num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        a=[]\n",
    "        n=0\n",
    "        for i in range(1,len(s)+1):\n",
    "            a.append(s[:i])\n",
    "        for j in words:\n",
    "            if j in a:\n",
    "                n+=1\n",
    "        return n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        target=[]\n",
    "        tmp=\"\"\n",
    "        for i in s:\n",
    "            tmp+=i\n",
    "            target.append(tmp)\n",
    "        res=0\n",
    "        for i in words:\n",
    "            if i in target:\n",
    "                res+=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        ans = 0\n",
    "        for i in range(1,len(s)+1):\n",
    "            ans += words.count(s[:i])\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        res = 0\n",
    "        for word in words:\n",
    "            if s.startswith(word):\n",
    "                res += 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        w = Counter(words)\n",
    "        num = 0\n",
    "        n = len(s)\n",
    "        for i in range(1,n+1):\n",
    "             if s[:i] in w:\n",
    "                 num += w[s[:i]]\n",
    "        return num\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        count = 0\n",
    "        for i in words:\n",
    "            len_i = len(i)\n",
    "            if i == s[:len_i]:\n",
    "                count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        ans, slen = 0, len(s)\n",
    "        for word in words:\n",
    "            canpass = True\n",
    "            for i in range(min(len(word), slen)):\n",
    "                if word[i] != s[i]:\n",
    "                    canpass = False\n",
    "                    break\n",
    "            if canpass and slen >=len(word):\n",
    "                ans += 1\n",
    "            print(\"word={}, canpass={}\".format(word, canpass))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        return sum([1 for w in words if s.startswith(w)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        res = 0   \n",
    "        for word in words:\n",
    "            if s.startswith(word):\n",
    "                res += 1\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        res = 0\n",
    "        for word in words:\n",
    "            if s.startswith(word):\n",
    "                res+=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        swim=0\n",
    "        a=len(s)\n",
    "        for i in words:\n",
    "            if s[0:len(i)]==i:\n",
    "                swim+=1            \n",
    "        return swim "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        res=0\n",
    "        for word in words:\n",
    "            if len(word)>len(s):\n",
    "                continue\n",
    "            if s[:len(word)]==word:\n",
    "                res+=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self,words: List[str], s: str) -> int:\n",
    "        count=0\n",
    "        for a in words:\n",
    "            check=True\n",
    "            if len(a)<=len(s):\n",
    "                for b in range(len(a)):\n",
    "                    if a[b]!=s[b]:\n",
    "                        check=False\n",
    "                        break\n",
    "            else:\n",
    "                check=False\n",
    "                continue\n",
    "            if check==True:\n",
    "                count=count+1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        cnt = 0\n",
    "        for word in words:\n",
    "            if s.startswith(word):\n",
    "                cnt += 1\n",
    "        return cnt\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        res = 0   # 符合要求字符串个数\n",
    "        for word in words:\n",
    "            if s.startswith(word):\n",
    "                res += 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        lst = []\n",
    "        cnt = 0\n",
    "        for i in range(1,len(s)+1):\n",
    "            lst.append(s[:i])\n",
    "        for x in words:\n",
    "            if x in lst:\n",
    "                cnt += 1\n",
    "                \n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        return sum(map(s.startswith,words))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        count=0\n",
    "        for i in words:\n",
    "            if s.find(i)==0:\n",
    "                count+=1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        ans = 0\n",
    "        for t in words:\n",
    "            if len(t) > len(s):\n",
    "                continue\n",
    "            if s.find(t) == 0:\n",
    "                ans +=1\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        res = 0   # 符合要求字符串个数\n",
    "        for word in words:\n",
    "            if s.startswith(word):\n",
    "                res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        res =[]\n",
    "        for v in words:\n",
    "            if len(v)<=len(s):\n",
    "                if v == s[:len(v)]:\n",
    "                    res.append(v)\n",
    "        return len(res)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        res = 0   # 符合要求字符串个数\n",
    "        for word in words:\n",
    "            if s.startswith(word):\n",
    "                res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        return sum(s.startswith(c) for c in words)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        res = 0\n",
    "        for i in words:\n",
    "            if i == s[:len(i)]:\n",
    "                res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        return sum([s.startswith(w) for w in words])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "\n",
    "        result = 0\n",
    "        for i in words:\n",
    "            if i == s[:len(i)]:\n",
    "                result += 1\n",
    "        \n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        # 直接按w的长度找s[:len(w)]==w\n",
    "        # res = 0\n",
    "        # for w in words:\n",
    "        #     n = len(w)\n",
    "        #     # print(s[:n], w)\n",
    "        #     if s[:n]==w:\n",
    "        #         res += 1\n",
    "        # return res\n",
    "\n",
    "\n",
    "        # 用startswitch\n",
    "        res = 0\n",
    "        for w in words:\n",
    "            if s.startswith(w):\n",
    "                res +=1 \n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        ans = 0\n",
    "        for t in words:\n",
    "            if s.find(t) == 0:\n",
    "                ans +=1\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        return sum([s.startswith(word) for word in words])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        ans = 0\n",
    "        s_len = len(s)\n",
    "        for word in words:\n",
    "            word_len = len(word)\n",
    "            if word_len <= s_len:\n",
    "                ans += 1\n",
    "                for i in range(word_len):\n",
    "                    if word[i] != s[i]:\n",
    "                        ans -= 1\n",
    "                        break\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countPrefixes(self, words: List[str], s: str) -> int:\n",
    "        cnt = 0\n",
    "        for elem in words:\n",
    "            if s.startswith(elem):\n",
    "                cnt += 1\n",
    "        return cnt"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
